package com.woniu.zsd.merchant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.woniu.client.PermissionClient;
import com.woniu.client.UserClient;
import com.woniu.dto.NoticeListDto;
import com.woniu.dto.UserDto;
import com.woniu.form.PublishNoticeForm;
import com.woniu.form.QueryNoticeForm;
import com.woniu.form.SelAdminInfoForm;
import com.woniu.form.UpdateNoticeForm;
import com.woniu.zsd.commons.core.model.Result;
import com.woniu.zsd.merchant.entity.Notice;
import com.woniu.zsd.merchant.mapper.NoticeMapper;
import com.woniu.zsd.merchant.service.INoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.zsd.merchant.util.DateTimeUtil;
import com.woniu.zsd.permission.entity.AdminInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private UserClient userClient;
    @Autowired
    private PermissionClient permissionClient;

    @Override
    public Page<NoticeListDto> noticeList(QueryNoticeForm queryNoticeForm) {
        Page<NoticeListDto> page = new Page<>(
                queryNoticeForm.getPageNum() == null ? 1 : queryNoticeForm.getPageNum(),
                queryNoticeForm.getPageSize() == null ? 5 : queryNoticeForm.getPageSize()
        );
        DateTime startTime = null;
        DateTime endTime = null;
        if (queryNoticeForm.getStartTime() != null && !queryNoticeForm.getStartTime().isEmpty()) {
            startTime = DateTimeUtil.stringToDateTime(queryNoticeForm.getStartTime());
        }

        if (queryNoticeForm.getEndTime() != null && !queryNoticeForm.getEndTime().isEmpty()) {
            endTime = DateTimeUtil.stringToDateTime(queryNoticeForm.getEndTime());
        }

        System.out.println(queryNoticeForm.getState());
        return noticeMapper.noticeList(page, queryNoticeForm.getTitle(), startTime, endTime, queryNoticeForm.getState());
    }

    @Override
    public void publishNotice(PublishNoticeForm publishNoticeForm, Integer adminId) {
        Notice notice = new Notice();
        BeanUtil.copyProperties(publishNoticeForm, notice);
        notice.setPublishUserId(adminId);
        Set<Integer> integers = new HashSet<>();
        integers.add(adminId);
        //通过 ID 查询姓名
        AdminInfo adminInfo = permissionClient.selectAdminInfoByIds(integers).getData().get(0);
        notice.setPublishUserName(adminInfo.getName());
        noticeMapper.insert(notice);
    }

    @Override
    public void deleteNotice(List<Integer> ids, Integer adminId) {
        // 批量删除并修改删除人
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        List<Notice> notices = noticeMapper.selectList(queryWrapper);

        // 通过 ID 查询姓名
        AdminInfo adminInfo = permissionClient.selectAdminInfoByIds(Collections.singleton(adminId)).getData().get(0);
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids)
                     .set("update_user_id", adminId)
                     .set("update_user_name", adminInfo.getName())
                     .set("is_delete", 0);

        noticeMapper.update(null, updateWrapper);
    }

    @Override
    public void updateNotice(UpdateNoticeForm updateNoticeForm, Integer adminId) {
        Notice notice = new Notice();
        BeanUtil.copyProperties(updateNoticeForm, notice);
        notice.setUpdateUserId(adminId);
        // 根据ID查询用户姓名
        AdminInfo adminInfo = permissionClient.selectAdminInfoByIds(Collections.singleton(adminId)).getData().get(0);
        notice.setUpdateUserName(adminInfo.getName());
        noticeMapper.updateById(notice);
    }

    @Override
    public List<NoticeListDto> draftNotice(Integer adminId) {
        return noticeMapper.draftNotice(adminId);
    }
}
